package com.ruihang.generalibrary.ui.widget;

import android.content.Context;
import android.support.v7.widget.AppCompatCheckBox;
import android.util.AttributeSet;

import com.ruihang.generalibrary.utils.FontProvider;

/**
 * Created by Administrator on 2016/12/16.
 */

public class CustomCheckBox extends AppCompatCheckBox {

    private OnCheckedChangeListener checkedChangeListener;
    private boolean isUseOuterFont;

    public CustomCheckBox(Context context) {
        super(context);
        if (isUseOuterFont) {
            setTypeface(FontProvider.getTypeface(context));
        }
    }

    public CustomCheckBox(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (isUseOuterFont) {
            setTypeface(FontProvider.getTypeface(context));
        }
    }

    public CustomCheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if (isUseOuterFont) {
            setTypeface(FontProvider.getTypeface(context));
        }
    }

    @Override
    public void setChecked(boolean checked) {
        super.setChecked(checked);
    }

    @Override
    public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
        super.setOnCheckedChangeListener(listener);
        this.checkedChangeListener = listener;
    }

    public void setChecked(boolean checked, boolean callListener) {
        if (callListener || this.checkedChangeListener == null) {
            setChecked(checked);
        } else {
            OnCheckedChangeListener temp = this.checkedChangeListener;
            setOnCheckedChangeListener(null);
            setChecked(checked);
            setOnCheckedChangeListener(temp);
        }
    }
}
